﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
namespace Design.ViewModel
{
    public class DynamicMenu:ViewModelBase
    {
        public List<AccordionItem> lstParentMenu;
        public HyperlinkButton testhyper;
        public List<AccordionItem> LstParentMenu
        {
            get { return lstParentMenu; }
            set { lstParentMenu = value; this.OnPropertyChanged("LstParentMenu"); }
        }

        public DynamicMenu()
        {
            UriAdd = new Uri("Page1", UriKind.Relative);
            //testhyper = new HyperlinkButton();
            //this.Source = "/Views/Datagrid.xaml";
            Style accorditonitemhyperlink = Application.Current.Resources["AccordionItemHyperlinkStyle"] as Style;
            Style accorditionittem= Application.Current.Resources["AccordionItemParentStyle"] as Style;
            Style hyperlinkmenuitem = Application.Current.Resources["HyperlinkButtonMenuitemStyle"] as Style;
            Style hyperlink = Application.Current.Resources["HyperlinkButtonMenuStyle"] as Style;
            List<AccordionItem> temp= new List<AccordionItem>();
            AccordionItem accParentMenu;//Parent Menu
            HyperlinkButton temp1=new HyperlinkButton();
            lstActive = new List<HyperlinkButton>();
            lstAccohyper = new List<AccordionItem>();
            StackPanel stackpanel = new StackPanel();
            temp1 = GetHyperlinkMenuItem("General", "/Views/General.xaml", hyperlinkmenuitem);
                stackpanel.Children.Add(temp1);
                lstActive.Add(temp1);
                temp1 = GetHyperlinkMenuItem("Datagrid", "/Views/Datagrid.xaml", hyperlinkmenuitem);
                stackpanel.Children.Add(temp1);
                lstActive.Add(temp1);



                accParentMenu = GetAccordionItemHyperlink("HOME", "/Views/Message.xaml", "", accorditonitemhyperlink);
                accParentMenu.ApplyTemplate();
            


                lstActive.Add(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(accParentMenu, 0) as Grid, 0) as HyperlinkButton);
            temp.Add(accParentMenu);
            accParentMenu = GetAccordionItemHyperlink("HOME2", "/Views/Message.xaml", "", accorditonitemhyperlink);
            accParentMenu.ApplyTemplate();
           
            
            lstActive.Add(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(accParentMenu, 0) as Grid, 0) as HyperlinkButton);
            temp.Add(accParentMenu);
            
           //  var tem = accParentMenu.FindName("hyperlinkButton") as HyperlinkButton;
           //  testhyper = tem;
           //// object te = accParentMenu.FindName("hyperlinkButton");
           //// testhyper = te as HyperlinkButton;
           //// lstActive.Add(testhyper);
            accParentMenu = GetAccordionItemParent("DashBoard", stackpanel, "/Design;component/22.png ", accorditionittem);
            temp.Add(accParentMenu);
            this.LstParentMenu = temp;

        }
        public AccordionItem GetAccordionItemParent(object header, object content, string uriIcon, Style style)
        {
            AccordionItem Items3 = new AccordionItem();
            Items3.Header = header;
            Items3.Content = content;
           
            Items3.Style = style;
            
            BitmapImage image = new BitmapImage();
            image.UriSource = new Uri(uriIcon, UriKind.Relative);
            ImageSource ima = image;
            Items3.SetValue(EyeCandy.ImageProperty, ima);
           // this.lstActive.Add(Items3);

            return Items3;
        }

        public AccordionItem GetAccordionItemHyperlink(object header, string uriLink, string uriIcon, Style style)
        {
            AccordionItem Items3 = new AccordionItem();
            Items3.Header = header;
            Items3.Content = uriLink;

            Items3.Style = style;
            
            Items3.Selected+=new RoutedEventHandler(accoclick);
            return Items3;
        }

        private void accoclick(object sender, RoutedEventArgs e)
        {
           
            foreach (var item in this.lstActive)
            {

                VisualStateManager.GoToState(item, "InActive", false);
            }
        }
       
        public AccordionItem GetAccordionItemHyperlink1(object header, string uriLink)
        {
            AccordionItem Items3 = new AccordionItem();
            Items3.Header = header;
            Items3.Content = uriLink;

          

            return Items3;
        }
        public HyperlinkButton GetHyperlinkMenuItem(object content,string uri, Style style)
        {
            HyperlinkButton Items3 = new HyperlinkButton();
            Items3.Content = content;
            Items3.Style = style;
            Items3.Click += new RoutedEventHandler(hblkCliked);
            Items3.NavigateUri = new Uri(uri,UriKind.Relative);
            Items3.TargetName = "testframe";
            return Items3;
        }
        public HyperlinkButton GetHyperlinkItem(object content, Style style)
        {
            HyperlinkButton Items3 = new HyperlinkButton();
            Items3.Content = content;
            Items3.Style = style;
            Items3.Width = 200;
            Items3.Height = 32;
            // this.lstActive.Add(Items3);
            return Items3;
        }
        private void setCurrentMenu()
        {
            foreach (var item in this.lstActive)
            {
                string pathUri = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
                string currentUri = this.source;
                if (pathUri.Substring(pathUri.Length - currentUri.Length).Equals(currentUri))
                {
                    
                    break;
                }
            }
        }
        private void hblkCliked(object sender, RoutedEventArgs e)
        {

            foreach (var item in this.lstActive)
            {
                
                VisualStateManager.GoToState(item , "InActive", false);
            }
            VisualStateManager.GoToState((sender as HyperlinkButton), "Active", false);
            
        }
        
        public Uri UriAdd { get; set; }
        public string source;
        public string Source
        {
            get { return this.source; }
            set { this.source = value;  OnPropertyChanged("Source"); }
        }
        public List<HyperlinkButton> lstActive;
        public List<AccordionItem> lstAccohyper;
    }
}
